package com.cy.util;

import java.util.List;

import com.cy.action.BackLogin;
import com.cy.entity.BackPurview;
import com.cy.entity.BackUserType;
import com.cy.service.BackPurviewService;
import com.cy.session.SessionHelper;
import com.cy.session.SessionUser;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor{
	private BackPurviewService backPurviewService;
	
	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		SessionUser sessionUser=SessionHelper.getSessionUser();
		String namespace = invocation.getProxy().getNamespace();
		String method = invocation.getProxy().getMethod();
		String actionName = invocation.getProxy().getActionName().toLowerCase();
		if(sessionUser==null &&!(invocation.getAction().getClass() == BackLogin.class) ){
			return "error_p";
		}
		
		List<BackPurview> ps=backPurviewService.listAllPurviewForSysAdmin();
		boolean flag=false;
		for (BackPurview purview:ps){
			if (StringUtils.isNotEmpty(purview.getActionName()) && purview.getActionName().toLowerCase().equals(actionName)){
				flag=true;
			}
		}
		
		if(flag){
			if(sessionUser.getBackUserType()!=BackUserType.systemAdmin){
				if(!sessionUser.checkPurview(actionName)){
					return "error_p";
				}
			}
		}
		return invocation.invoke();	
	}

	public BackPurviewService getBackPurviewService() {
		return backPurviewService;
	}

	public void setBackPurviewService(BackPurviewService backPurviewService) {
		this.backPurviewService = backPurviewService;
	}
	
	

}
